package com.acce.gxsc.page.outCarList;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.acce.gxsc.R;
import com.acce.gxsc.model.netBean.InCarBean;
import com.acce.gxsc.page.inCarDetail.InCarActivity;
import com.acce.sdk.BaseActivity;
import com.acce.sdk.base.mvp.BaseMvpFragment;
import com.acce.sdk.widget.adapterrv.CommonAdapter;
import com.acce.sdk.widget.adapterrv.ViewHolder;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by huxley on 2017/5/26.
 */

public class OutCarListFragment extends BaseMvpFragment<OutCarListView, OutCarListPresenter> implements OutCarListView {

    View rootView;
    @Bind(R.id.rv_goods_prediction)
    RecyclerView mRvGoodsPrediction;

    CommonAdapter<InCarBean> mCommonAdapter;
    ArrayList<InCarBean>     data;

    public static OutCarListFragment newInstance() {
        Bundle args = new Bundle();
        OutCarListFragment fragment = new OutCarListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected OutCarListPresenter createPresenter() {
        return new OutCarListPresenter();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_out_car_list, container, false);
        ButterKnife.bind(this, rootView);
        init();
//        mPresenter.loadBatchDetail(null);
        return rootView;
    }

    private void init() {
        data = new ArrayList<>();
        mRvGoodsPrediction.setLayoutManager(new LinearLayoutManager(getContext()));
        mRvGoodsPrediction.setAdapter(mCommonAdapter = new CommonAdapter<InCarBean>(getContext(), R.layout.item_in_car, data) {
            @Override
            protected void convert(ViewHolder holder, final InCarBean saleSheet, final int position) {
                holder.setText(R.id.tv_send_company, saleSheet.consignor);
                holder.setText(R.id.tv_time, saleSheet.createTime.substring(11, 19));
                holder.setText(R.id.tv_car_no, saleSheet.plateNum);
                holder.setText(R.id.tv_box_no, saleSheet.containNo);
                holder.setText(R.id.tv_goods_info, saleSheet.goodsInfo);
                holder.setOnClickListener(R.id.rl_content, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(OutCarListFragment.this.getActivity(), InCarActivity.class);
                        intent.putExtra("inCarKey", saleSheet.inCarKey);
                        OutCarListFragment.this.getActivity().startActivity(intent);
                    }
                });
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);

    }

    @Override
    public void showProgressDialog(String s, boolean b) {
        ((BaseActivity) getActivity()).showProgressDialog(s, b);
    }

    @Override
    public void hideProgressDialog() {
        ((BaseActivity) getActivity()).hideProgressDialog();
    }

    @Override
    public void loadInCarListSuccess(ArrayList<InCarBean> inCarList) {
        mCommonAdapter.getDatas().clear();
        if (inCarList != null) {
            mCommonAdapter.getDatas().addAll(inCarList);
        }
        mCommonAdapter.notifyDataSetChanged();
    }
}
